package com.ruoyi.web.controller.gcgl;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gcgl.domain.GysptMaterial;
import com.ruoyi.gcgl.domain.GysptMaterialType;
import com.ruoyi.gcgl.service.IGysptMaterialService;
import com.ruoyi.gcgl.service.IGysptMaterialTypeService;

/**
 * 材料规格Controller
 * 
 * @author ruoyi
 * @date 2024-10-02
 */
@Controller
@RequestMapping("/gcgl/material")
public class GysptMaterialController extends BaseController
{
    private String prefix = "gcgl/material";

    @Autowired
    private IGysptMaterialService gysptMaterialService;
    @Autowired
    private IGysptMaterialTypeService gysptMaterialTypeService;

    @RequiresPermissions("gcgl:material:view")
    @GetMapping()
    public String material(ModelMap mmap)
    {
    	GysptMaterialType gysptMaterialType= new GysptMaterialType();
    	gysptMaterialType.setDeleteFlag(1);
    	List<GysptMaterialType> typelis = gysptMaterialTypeService.selectGysptMaterialTypeList(gysptMaterialType);
    	mmap.put("typelis", typelis);
        return prefix + "/material";
    }

    /**
     * 查询材料规格列表
     */
//    @RequiresPermissions("gcgl:material:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GysptMaterial gysptMaterial)
    {
        startPage();
        gysptMaterial.setDeleteFlag(1);
        List<GysptMaterial> list = gysptMaterialService.selectGysptMaterialList(gysptMaterial);
        return getDataTable(list);
    }

    /**
     * 导出材料规格列表
     */
    @RequiresPermissions("gcgl:material:export")
    @Log(title = "材料规格", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GysptMaterial gysptMaterial)
    {
        List<GysptMaterial> list = gysptMaterialService.selectGysptMaterialList(gysptMaterial);
        ExcelUtil<GysptMaterial> util = new ExcelUtil<GysptMaterial>(GysptMaterial.class);
        return util.exportExcel(list, "材料规格数据");
    }

    /**
     * 新增材料规格
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
    	GysptMaterialType gysptMaterialType= new GysptMaterialType();
    	gysptMaterialType.setDeleteFlag(1);
    	List<GysptMaterialType> typelis = gysptMaterialTypeService.selectGysptMaterialTypeList(gysptMaterialType);
    	mmap.put("typelis", typelis);
        return prefix + "/add";
    }

    /**
     * 新增保存材料规格
     */
    @RequiresPermissions("gcgl:material:add")
    @Log(title = "材料规格", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GysptMaterial gysptMaterial)
    {
    	GysptMaterialType gysptMaterialType = 
    			gysptMaterialTypeService.selectGysptMaterialTypeById(gysptMaterial.getTypeId());
    	gysptMaterial.setTypeName(gysptMaterialType.getName());
    	gysptMaterial.setDeleteFlag(1);
    	gysptMaterial.setCreateAt(new Date());
    	gysptMaterial.setCreateUser(getLoginName());
    	gysptMaterial.setUpdateAt(new Date());
    	gysptMaterial.setUpdateUser(getLoginName());
        return toAjax(gysptMaterialService.insertGysptMaterial(gysptMaterial));
    }

    /**
     * 修改材料规格
     */
    @RequiresPermissions("gcgl:material:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GysptMaterial gysptMaterial = gysptMaterialService.selectGysptMaterialById(id);
        mmap.put("gysptMaterial", gysptMaterial);
        return prefix + "/edit";
    }

    /**
     * 修改保存材料规格
     */
    @RequiresPermissions("gcgl:material:edit")
    @Log(title = "材料规格", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GysptMaterial gysptMaterial){
    	
    	GysptMaterialType gysptMaterialType = 
    			gysptMaterialTypeService.selectGysptMaterialTypeById(gysptMaterial.getTypeId());
    	gysptMaterial.setTypeName(gysptMaterialType.getName());
    	gysptMaterial.setUpdateAt(new Date());
    	gysptMaterial.setUpdateUser(getLoginName());
        return toAjax(gysptMaterialService.updateGysptMaterial(gysptMaterial));
    }

    /**
     * 删除材料规格
     */
    @RequiresPermissions("gcgl:material:remove")
    @Log(title = "材料规格", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gysptMaterialService.deleteGysptMaterialByIds(ids));
    }
    
    //基价修改
    @GetMapping("/materialJjxg")
    public String materialJjxg(ModelMap mmap){
    	GysptMaterialType gysptMaterialType= new GysptMaterialType();
    	gysptMaterialType.setDeleteFlag(1);
    	List<GysptMaterialType> typelis = gysptMaterialTypeService.selectGysptMaterialTypeList(gysptMaterialType);
    	mmap.put("typelis", typelis);
        return prefix + "/materialJjxg";
    }

    /**
     * 查询基价修改列表
     */
    @PostMapping("/jjxglist")
    @ResponseBody
    public TableDataInfo jjxglist(GysptMaterial gysptMaterial){
        startPage();
        gysptMaterial.setDeleteFlag(1);
        gysptMaterial.setShul("1");
        List<GysptMaterial> list = gysptMaterialService.selectGysptMaterialList(gysptMaterial);
        return getDataTable(list);
    }
    
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap){
        GysptMaterial gysptMaterial = gysptMaterialService.selectGysptMaterialById(id);
        mmap.put("gysptMaterial", gysptMaterial);
        
        return prefix + "/detail";
    }
    
}